package com.example.skincoredemo;

import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.core.view.ViewCompat;

import com.example.skincoredemo.utils.SkinResources;
import com.example.skincoredemo.utils.SkinThemeUtils;

import java.util.ArrayList;
import java.util.List;

// 换肤属性处理类
public class SkinAttribute {

    private static final List<String> mAttribute = new ArrayList<>();

    private List<SkinView> skinViews = new ArrayList<>();

    static {

        mAttribute.add("background");

        mAttribute.add("src");

        mAttribute.add("textColor");

        mAttribute.add("drawableLeft");
        mAttribute.add("drawableTop");
        mAttribute.add("drawableRight");
        mAttribute.add("drawableBottom");
    }

    public void load(View view, AttributeSet attributeSet) {

        ArrayList<SkinPain> skinPains = new ArrayList<>();

        for (int i = 0; i < attributeSet.getAttributeCount(); i++) {
            // 获取属性名字
            String attributeName = attributeSet.getAttributeName(i);
            if (mAttribute.contains(attributeName)) {
                // 获取属性对应的值
                String attributeValue = attributeSet.getAttributeValue(i);
                // 判断属性值是否写死，跳过写死的内容属性
                if (attributeValue.startsWith("#")) {
                    continue;
                }
                // 获取属性值
                int resId = 0;
                if (attributeValue.startsWith("?")) {
                    int attrId = Integer.parseInt(attributeValue.substring(1));
                    SkinThemeUtils.getResId(view.getContext(), new int[]{attrId});
                } else { // @ 开头的
                    resId = Integer.parseInt(attributeValue.substring(1));
                }

                if (resId != 0) {
                    SkinPain skinPain = new SkinPain(attributeName, resId);
                    skinPains.add(skinPain);
                }
            }
        }
        if (!skinPains.isEmpty() || view instanceof TextView || view instanceof SkinViewSupport) {
            SkinView skinView = new SkinView(view, skinPains);
            skinView.applySkin();
            skinViews.add(skinView);
        }
    }

    static class SkinView {

        View view;
        List<SkinPain> skinPains;

        public SkinView(View view, List<SkinPain> skinPains) {
            this.view = view;
            this.skinPains = skinPains;
        }

        // 设置及应用皮肤
        public void applySkin() {
            for (SkinPain skinPain : skinPains) {
                Drawable left = null, top = null, right = null, bottom = null;
                switch (skinPain.attributeName) {
                    case "background":
                        Object background = SkinResources.getInstance().getBackground(skinPain.resId);
                        if (background instanceof Integer) {
                            view.setBackgroundColor((Integer) background);
                        } else {
                            ViewCompat.setBackground(view, (Drawable) background);
                        }
                        break;
                    case "src":
                        background = SkinResources.getInstance().getBackground(skinPain.resId);
                        if (background instanceof Integer) {
                            ((ImageView) view).setImageDrawable(new ColorDrawable((Integer) background));
                        } else {
                            ((ImageView) view).setImageDrawable((Drawable) background);
                        }
                        break;
                    case "textColor":
                        ((TextView) view).setTextColor(SkinResources.getInstance().getColorStateList(skinPain.resId));
                        break;
                    case "drawableLeft":
                        left = SkinResources.getInstance().getDrawable(skinPain.resId);
                        break;
                    case "drawableTop":
                        top = SkinResources.getInstance().getDrawable(skinPain.resId);
                        break;
                    case "drawableRight":
                        right = SkinResources.getInstance().getDrawable(skinPain.resId);
                        break;
                    case "drawableBottom":
                        bottom = SkinResources.getInstance().getDrawable(skinPain.resId);
                        break;
                    default:
                        break;
                }
                if (null != left || null != right || null != top || null != bottom) {
                    ((TextView) view).setCompoundDrawablesWithIntrinsicBounds(left, top, right, bottom);
                }
            }
        }
    }

    static class SkinPain {
        String attributeName;
        int resId;

        public SkinPain(String attributeName, int resId) {
            this.attributeName = attributeName;
            this.resId = resId;
        }
    }

    /**
     * 换皮肤
     */
    public void applySkin() {
        for (SkinView skinView : skinViews) {
            skinView.applySkin();
        }
    }

}
